<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>留言管理</cite></a>
        <a><cite>部门联系人</cite></a>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-fluid">
            <div class="layui-card">
                <div class="layui-card-body">
                    <table class="layui-hide" id="table-instance" lay-filter="table-instance"></table>
                    <script type="text/html" id="imgTpl">
                        <img style="display: inline-block; width: 50%; height: 100%;" src={ { d.avatar }}>
                    </script>
                    <script type="text/html" id="table-useradmin-webuser">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['layedit', 'admin', 'upload', 'laydate', 'useradmin', 'table', 'form'], function() {
        var $ = layui.$,
            admin = layui.admin,
            setter = layui.setter,
            view = layui.view,
            table = layui.table,
            form = layui.form,
            leyer = layui.leyer,
            upload = layui.upload,
            laydate = layui.laydate,
            layedit = layui.layedit;

        form.render(null, 'layadmin-userfront-formlist');
        //监听搜索
        form.on('submit(LAY-user-front-search)', function(data) {
            var field = data.field;
            //执行重载
            table.reload('table-instance', {
                where: field
            });
        });

        table.render({
            elem: "#table-instance",
            url: setter.request.baseUrl + "/contactus/admin/list",
            method: "get",
            toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板  
            headers: {
                token: layui.data("layuiAdmin").token
            },
            response: {
                statusName: "code",
                statusCode: 0,
                msgName: "msg",
                countName: "count",
                dataName: "list",
            },
            where: {
                typeId: 1
            },
            cols: [
                [{
                    align: 'center',
                    field: "id",
                    width: "7%",
                    title: "ID",
                    sort: true
                }, {
                    align: 'center',
                    field: "departmentName",
                    width: "15%",
                    title: "部门名称"
                }, {
                    align: 'center',
                    field: "phone",
                    width: "9%",
                    title: "电话"
                }, {
                    align: 'center',
                    field: "email",
                    width: "9%",
                    title: "e-mail"
                }, {
                    align: 'center',
                    field: "address",
                    width: "15%",
                    title: "地址"
                }, {
                    align: 'center',
                    toolbar: "#table-useradmin-webuser" //删除
                }]
            ],
            page: true, //分页
        });

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


        //删除模块
        table.on("tool(table-instance)", function(obj) {
            var data = obj.data;

            if (obj.event === "del") {
                layer.confirm("真的删除么", function(index) {
                    delAdminHandle({
                        id: data.id,
                    });

                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === "edit") {
                admin.popup({
                    title: "编辑联系人",
                    area: ["500px", "500px"],
                    success: function(layero, index) {

                        view(this.id)
                            .render("message/table", data)
                            .done(function() {
                                let _data = data;
                                console.log(_data)
                                form.render(null, "layuiadmin-form-admin");
                                //监听提交
                                form.on("submit(LAY-user-front-submit)", function(data) {
                                    var field = data.field;
                                    editUserHandle(Object.assign(_data, field));
                                    //提交 Ajax 成功后，关闭当前弹层并重载表格
                                    // adminAdd(field);

                                    layer.close(index);
                                });
                            });
                    },
                });
            }
        });

        //删除
        function delAdminHandle(params) {
            console.log(params)

            //  debugger
            admin.req({
                //*****************需要替换****************
                url: setter.request.baseUrl + "/contactus/admin/delete",
                data: {
                    ...params,
                },
                type: "get",
                done: function(res) {
                    layer.msg("删除成功"); //删除提示框
                },
            });
        }

        //修改编辑
        function editUserHandle(params) {
            admin.req({
                url: setter.request.baseUrl + "/contactus/admin/save",
                data: JSON.stringify(params),
                type: "post",
                dataType: "json",
                contentType: "application/json",
                done: function(res) {
                    layui.table.reload("table-instance");
                },
            });
        }

    });
</script>